home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / OFFSCREE / OFFSCREE.C next >
Text File  |  1992-01-01  |  4KB  |  136 lines

  1. /*
  2.  *    offScreenBitMap.c        Copyright ⌐ 1990 Brigham Stevens
  3.  *    -----------------
  4.  *
  5.  *    This file contains routines for working with off screen bitmaps.
  6.  *
  7.  *    NewBitMap            -    Allocates an off screen BitMap from a given Rect.
  8.  *    CalcOffScreen        -    Calculates fields of a BitMap for a given Rect.
  9.  *    FreeBitMap            -    Frees allocated memory in an off screen BitMap.
  10.  *    DrawBitMap            -    Draw a BitMap in the current GrafPort.
  11.  *    LoadPicture            -    Makes an off screen BitMap from a PICT resource.
  12.  *
  13.  */
  14.  
  15. #include "offScreenBitMap.h"
  16.  
  17. /*------------------------------------------------------------------------------
  18.  *    NewBitMap
  19.  *    Allocates an off Screen bitmap with the portBits of frame.
  20.  *    The BitMap is returned with all the fields initialized.  Check MemErr
  21.  *    after calling NewBitMap to make sure the memory was allocated.
  22.  *
  23.  *------------------------------------------------------------------------------*/
  24.  
  25. void NewBitMap(frame,theMap)
  26. Rect *frame;
  27. BitMap *theMap;
  28. {
  29.     int        size;
  30.     int        rbytes;
  31.     CalcOffScreen(frame,&size,&rbytes);
  32.     theMap->rowBytes=rbytes;
  33.     theMap->bounds=*frame;
  34.     theMap->baseAddr=NewPtr(size);
  35. }
  36.  
  37. /*------------------------------------------------------------------------------
  38.  *    CalcOffScreen
  39.  *    Calculate the number of bytes needed and the
  40.  *    rowBytes field for a BitMap that has the portBits of frame.
  41.  *
  42.  *------------------------------------------------------------------------------*/
  43.  
  44. void CalcOffScreen(frame,needed,rows)
  45. register Rect        *frame;
  46. register int        *needed;
  47. register int        *rows;
  48. {
  49.     *rows=((((frame->right) - (frame->left)) + 15)/16) *2;
  50.     *needed=((*rows) * ((frame->bottom) - (frame->top)));
  51. }
  52.  
  53.  
  54. /*------------------------------------------------------------------------------
  55.  *    FreeBitMap
  56.  *    Deallocates an offscreen BitMap and sets all the fields
  57.  *    to Zero.  Call when you are completely done with a BitMap that was
  58.  *    allocated with NewBitMap.
  59.  *
  60.  *------------------------------------------------------------------------------*/
  61.  
  62. void FreeBitMap(Bits)
  63. BitMap *Bits;
  64. {
  65.     DisposPtr(Bits->baseAddr);
  66.     Bits->baseAddr=NIL;
  67.     SetRect(&Bits->bounds,0,0,0,0);
  68.     Bits->rowBytes=0;
  69. }
  70.  
  71. /*------------------------------------------------------------------------------
  72.  *    DrawBitMap
  73.  *    This will Draw the BitMap passed to in the current grafPort
  74.  *    in rectangle frame using the bit Transfer mode passed.
  75.  *
  76.  *------------------------------------------------------------------------------*/
  77.  
  78. void DrawBitMap(bits,frame,mode)
  79. BitMap *bits;
  80. Rect *frame;
  81. int    mode;
  82. {
  83.     CopyBits(bits,                    /* from bitmap        */
  84.             &(thePort->portBits),    /* to bitmap        */
  85.              &(bits->bounds),        /* from rect        */
  86.              frame,                    /* to rect            */
  87.              mode,                    /* transfer mode    */
  88.              NIL);                    /* Mask Region        */
  89. }
  90.  
  91.  
  92.  
  93. /*------------------------------------------------------------------------------
  94.  *    LoadPicture
  95.  *    Creates an offscreen bitmap and draws the picture
  96.  *    with the passed resID into it.  Then the pict is deallocated.
  97.  *    be sure to call SetPort after calling this routine, because 
  98.  *    thePort is invalid when it exits.  If the memory for the off screen
  99.  *    bitmap cannot be allocated then the routine cleans up and exits, setting
  100.  *    the BaseAddr field of the BitMap to zero.
  101.  *
  102.  *------------------------------------------------------------------------------*/
  103.  
  104. void LoadPicture(resID,theMap)
  105.     int     resID;
  106.     BitMap     *theMap;
  107. {
  108.         PicHandle    pict;
  109.         Rect        r;
  110.         GrafPort    tempPort;
  111.  
  112.         pict=GetPicture(resID);            
  113.         if(ResError()) 
  114.         {
  115.             theMap->baseAddr=NIL;
  116.             return;
  117.         }
  118.         r=(**pict).picFrame;
  119.         
  120.         NewBitMap(&r,theMap);
  121.         if(MemErr)
  122.         {
  123.             theMap->baseAddr=NIL;
  124.             DisposHandle(pict);
  125.             return;
  126.         }
  127.  
  128.         OpenPort(&tempPort);
  129.         SetPort(&tempPort);
  130.         SetPortBits(theMap);
  131.         FillRect(&tempPort.portRect,white);
  132.         DrawPicture(pict,&r);
  133.         DisposHandle(pict);
  134.         ClosePort(&tempPort);
  135. }
  136.